﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using JohnnySins.DAL;
using JohnnySins.DAL.Repositories;
using JohnnySins.Entities;
using JohnnySins.Code;

namespace JohnnySins.Admin
{
    public partial class AboutMe : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadAboutMe();
                LoadProfile();
                LoadBanner();
            }
        }

        protected void LoadAboutMe()
        {
            using (var repo = RepositoryFactory.ResolveRepository<ParameterSiteRepository>())
            {
                txtAboutMe.Text = "";
                ParameterSite itm = repo.Find("AboutMe");
                if ((itm != null) && (!string.IsNullOrEmpty(itm.ParameterValueStr)))
                    txtAboutMe.Text = itm.ParameterValueStr;
            }
        }

        protected void LoadProfile()
        {
            using (var repo = RepositoryFactory.ResolveRepository<ParameterSiteRepository>())
            {
                txtEthnicity.Text = "";
                ParameterSite itm = repo.Find("AboutMe_Ethnicity");
                if ((itm != null) && (!string.IsNullOrEmpty(itm.ParameterValueStr)))
                    txtEthnicity.Text = itm.ParameterValueStr;

                txtCountryOfOrigin.Text = "";
                itm = repo.Find("AboutMe_CountryOfOrigin");
                if ((itm != null) && (!string.IsNullOrEmpty(itm.ParameterValueStr)))
                    txtCountryOfOrigin.Text = itm.ParameterValueStr;

                txtState.Text = "";
                itm = repo.Find("AboutMe_State");
                if ((itm != null) && (!string.IsNullOrEmpty(itm.ParameterValueStr)))
                    txtState.Text = itm.ParameterValueStr;

                txtPlaceOfBirth.Text = "";
                itm = repo.Find("AboutMe_PlaceOfBirth");
                if ((itm != null) && (!string.IsNullOrEmpty(itm.ParameterValueStr)))
                    txtPlaceOfBirth.Text = itm.ParameterValueStr;

                txtCity.Text = "";
                itm = repo.Find("AboutMe_City");
                if ((itm != null) && (!string.IsNullOrEmpty(itm.ParameterValueStr)))
                    txtCity.Text = itm.ParameterValueStr;

                txtDateOfBirth.Text = "";
                itm = repo.Find("AboutMe_DateOfBirth");
                if ((itm != null) && (!string.IsNullOrEmpty(itm.ParameterValueStr)))
                    txtDateOfBirth.Text = itm.ParameterValueStr;

                txtEyeColor.Text = "";
                itm = repo.Find("AboutMe_EyeColor");
                if ((itm != null) && (!string.IsNullOrEmpty(itm.ParameterValueStr)))
                    txtEyeColor.Text = itm.ParameterValueStr;

                txtHairColor.Text = "";
                itm = repo.Find("AboutMe_HairColor");
                if ((itm != null) && (!string.IsNullOrEmpty(itm.ParameterValueStr)))
                    txtHairColor.Text = itm.ParameterValueStr;

                txtHeight.Text = "";
                itm = repo.Find("AboutMe_Height");
                if ((itm != null) && (!string.IsNullOrEmpty(itm.ParameterValueStr)))
                    txtHeight.Text = itm.ParameterValueStr;

                txtWeight.Text = "";
                itm = repo.Find("AboutMe_Weight");
                if ((itm != null) && (!string.IsNullOrEmpty(itm.ParameterValueStr)))
                    txtWeight.Text = itm.ParameterValueStr;

                txtMyTwitter.Text = "";
                itm = repo.Find("AboutMe_MyTwitter");
                if ((itm != null) && (!string.IsNullOrEmpty(itm.ParameterValueStr)))
                    txtMyTwitter.Text = itm.ParameterValueStr;

                txtWebsiteTwitter.Text = "";
                itm = repo.Find("AboutMe_WebsiteTwitter");
                if ((itm != null) && (!string.IsNullOrEmpty(itm.ParameterValueStr)))
                    txtWebsiteTwitter.Text = itm.ParameterValueStr;
            }
        }

        protected void LoadBanner()
        {
            using (var repo = RepositoryFactory.ResolveRepository<BannerRepository>())
            {
                gridBanner.DataSource = repo.FindAll("AboutMe");
                gridBanner.DataBind();
            }
        }

        protected void upAboutMe_Load(object sender, EventArgs e)
        {
        }

        protected void btnSaveAboutMe_Click(object sender, EventArgs e)
        {
            using (var repo = RepositoryFactory.ResolveRepository<ParameterSiteRepository>())
            {
                repo.Edit(new ParameterSite() { ParameterName = "AboutMe", ParameterValueStr = txtAboutMe.Text });
            }
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Alert", string.Format("alert('{0}');", "About Me saved"), true);
        }

        protected void btnCancelAboutMe_Click(object sender, EventArgs e)
        {
            LoadAboutMe();
        }

        protected void btnSaveProfile_Click(object sender, EventArgs e)
        {
            using (var repo = RepositoryFactory.ResolveRepository<ParameterSiteRepository>())
            {
                repo.Edit(new ParameterSite() { ParameterName = "AboutMe_Ethnicity", ParameterValueStr = txtEthnicity.Text });
                repo.Edit(new ParameterSite() { ParameterName = "AboutMe_CountryOfOrigin", ParameterValueStr = txtCountryOfOrigin.Text });
                repo.Edit(new ParameterSite() { ParameterName = "AboutMe_State", ParameterValueStr = txtState.Text });
                repo.Edit(new ParameterSite() { ParameterName = "AboutMe_PlaceOfBirth", ParameterValueStr = txtPlaceOfBirth.Text });
                repo.Edit(new ParameterSite() { ParameterName = "AboutMe_City", ParameterValueStr = txtCity.Text });
                repo.Edit(new ParameterSite() { ParameterName = "AboutMe_DateOfBirth", ParameterValueStr = txtDateOfBirth.Text });
                repo.Edit(new ParameterSite() { ParameterName = "AboutMe_EyeColor", ParameterValueStr = txtEyeColor.Text });
                repo.Edit(new ParameterSite() { ParameterName = "AboutMe_HairColor", ParameterValueStr = txtHairColor.Text });
                repo.Edit(new ParameterSite() { ParameterName = "AboutMe_Height", ParameterValueStr = txtHeight.Text });
                repo.Edit(new ParameterSite() { ParameterName = "AboutMe_Weight", ParameterValueStr = txtWeight.Text });
                repo.Edit(new ParameterSite() { ParameterName = "AboutMe_MyTwitter", ParameterValueStr = txtMyTwitter.Text });
                repo.Edit(new ParameterSite() { ParameterName = "AboutMe_WebsiteTwitter", ParameterValueStr = txtWebsiteTwitter.Text });
            }
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Alert", string.Format("alert('{0}');", "Profile saved"), true);
        }

        protected void btnCancelProfile_Click(object sender, EventArgs e)
        {
            LoadProfile();
        }

        protected void btnBannerUpload_Click(object sender, EventArgs e)
        {
            if (!fuBanner.HasFile)
                return;

            Banner itm = new Banner();
            itm.BannerFileName = fuBanner.FileName;
            itm.IsActive = true;
            itm.PageName = "AboutMe";

            using (var repo = RepositoryFactory.ResolveRepository<BannerRepository>())
            {
                repo.Insert(itm);

                string filePath = Path.Combine(Server.MapPath("~/Files/ImageBanner/AboutMe"), itm.PhysFileName);
                fuBanner.SaveAs(filePath);
            }
            LoadBanner();
        }

        protected void gridBanner_RowDataBound(Object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Banner item = (Banner)e.Row.DataItem;

                ImageButton ibtn = (ImageButton)e.Row.FindControl("btnDeactivate");
                ibtn.Visible = item.IsActive;

                ibtn = (ImageButton)e.Row.FindControl("btnActivate");
                ibtn.Visible = !item.IsActive;
            }
        }

        protected void gridBanner_Command(object sender, CommandEventArgs e)
        {
            int id = Convert.ToInt32(e.CommandArgument.ToString());

            if (e.CommandName == "deleteBanner")
            {
                DeleteBanner(id);
            }
            else
            {
                using (var repo = RepositoryFactory.ResolveRepository<BannerRepository>())
                {
                    switch (e.CommandName)
                    {
                        case "deactivate":
                            repo.Deactivate(id);
                            break;
                        case "activate":
                            repo.Activate(id);
                            break;
                    }
                }
            }

            LoadBanner();
        }

        protected void DeleteBanner(int id)
        {
            using (var repo = RepositoryFactory.ResolveRepository<BannerRepository>())
            {
                Banner itm = repo.Find(id);
                if (itm != null)
                {
                    string path = Path.Combine(Server.MapPath("~/Files/ImageBanner/AboutMe"), itm.PhysFileName);
                    if (File.Exists(path))
                        try
                        {
                            File.Delete(path);
                        }
                        catch (Exception ex) { }
                    repo.Delete(id);
                }
            }
        }
    }
}